﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.IO;

namespace TRAC_Demo
{
    static class ColorKeeper
    {
        #region [ Fields ]

        private static XDocument Colors;

        #endregion

        #region [ Methods ]

        // Determines whether to load or create the required xml document
        public static void Intialize()
        {
            if (File.Exists("colors.xml"))
            {
                LoadXDoc();
            }
            else
            {
                CreateXDoc();
            }
        }

        // Loads the xml document
        private static void LoadXDoc()
        {
            Colors = XDocument.Load("colors.xml");
        }

        // Creates the ck_color xml document
        private static void CreateXDoc()
        {
            // Array of default color names
            string[] colorNames = new string[] { "Red", "Blue", "Green", "Yellow", "Brown", "Gray", "Pink", "Purple", "Default" };

            // Multi Dimensional array with the argb values for the default colors
            int[,] colorValues = new int[9, 4] { 
            { 255, 255, 0, 0 }, // Red
            { 255, 0, 0, 255 }, // Blue
            { 255, 0, 128, 0 }, // Green
            { 255, 255, 255, 0 }, // Yellow
            { 255, 165, 42, 42 }, // Brown
            { 255, 128, 128, 128 }, // Gray
            { 255, 255, 192, 203 }, // Pink
            { 255, 128, 0, 128 }, // Purple
            { 255, 236, 233, 216 } // Control (default form color)
            };

            Colors = new XDocument(new XElement("Colors"));

            // Adds all the default colors to the colors xml document
            for (int i = 0; i < 9; i++)
            {
                Colors.Element("Colors").Add(new XElement("Color", new XAttribute("name", colorNames[i]),
                new XElement("Alpha", colorValues[i, 0]),
                new XElement("Red", colorValues[i, 1]),
                new XElement("Blue", colorValues[i, 2]),
                new XElement("Green", colorValues[i, 3])));
            }

            Colors.Declaration = new XDeclaration("1.0", "utf-8", "yes");
            Colors.Save("colors.xml");
        }

        // Looks for the specified color from the ck_color xml doc and returns it's argb values in a list
        public static List<int> RetrieveColor(string colorName)
        {
            List<int> colorValues = new List<int>();

            // Looks for the given color using a Lambda function
            XElement color = Colors.Element("Colors").Descendants("Color").Where(x => x.Attribute("name").Value == colorName).Single();

            colorValues.Add(int.Parse(color.Element("Alpha").Value));
            colorValues.Add(int.Parse(color.Element("Red").Value));
            colorValues.Add(int.Parse(color.Element("Blue").Value));
            colorValues.Add(int.Parse(color.Element("Green").Value));

            return colorValues;
        }

        #endregion
    }
}
